package com.dawnling.androidutils.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * 自定义指示器
 * twoIndicator = (MyViewPagerIndicator) headTwoViewPager.findViewById(R.id.two_myindicator);
 mTwoPager.addOnPageChangeListener(new OnPageChangeListener()
 {

 @Override // 滑动完成之后
 public void onPageSelected(int position){}

 @Override//当滑动时触发
 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels){
 twoIndicator.move(position,positionOffset);
 }

 @Override
 public void onPageScrollStateChanged(int state){}
 });
 twoIndicator.setPagerCount(mPagerCountTwo);
 */
public class MyViewPagerIndicator extends View
{
	/*从中间开始画公式：
	 * num = 5 ：自定义圆的个数
	 * radiu：半径
	 * for (int i = 0; i < num; i++)
	  {
	      canvas.getWidth()/2-(num-1)*1.5f*radiu+3*radiu*i
	  }
	  */
	private Paint mPaint;//初始画笔   空心圆
	private Paint fillPaint;//实心圆 画笔
	private int num = 2; //圆的个数
	private int radiu = 3; //半径
	private float offset;//偏移量

	public MyViewPagerIndicator(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initPaint();
	}

	// 新建画笔
	private void initPaint(){
		//默认文本
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setColor(Color.parseColor("#66a0a0a0"));// 设置颜色
		//触摸按下文本
		fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		fillPaint.setStyle(Paint.Style.FILL);
		fillPaint.setColor(Color.parseColor("#ffaf0000"));// 设置颜色
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);//canvas画布
		int y = canvas.getHeight();
		int x = canvas.getWidth();
		//空心圆
		for (int i = 0; i < num; i++)
		{
			canvas.drawCircle(x/2-(num-1)*1.5f*radiu+3*radiu*i, y-20, radiu, mPaint);
		}
		//实心圆
		canvas.drawCircle(x/2-(num-1)*1.5f*radiu+offset, y-20, radiu, fillPaint);
	}
	
	public void move(int position)
	{
		offset = 3*radiu*position;
		invalidate();
	}
	
	public void move(int position,float positionOffset)
	{
		position %= num;
		offset = 3*radiu*position+3*radiu*positionOffset;
		if (position == num-1)
		{
			offset = 3*radiu*position;
		}
		invalidate();
	}
	
	public void setPagerCount(int i) {
		num = i;
	}
}